﻿using System;
using System.Globalization;
using Medianamik.Core.Interfaces;


namespace Medianamik.Core
{
    public class NodeRepository : INodeRepository
    {
        public INodeWrapper NewNode(IBaseType dto)
        {
            return NodeManager.NewNode<NodeWrapper>(dto.TypeId, dto.Culture, dto.ParentNodeId);
        }

        public INodeWrapper GetExistingNode(IBaseType dto)
        {
            return NodeManager.GetNode<NodeWrapper>(dto.NodeId);
        }

        public void SaveNode(INodeWrapper node)
        {
            NodeManager.SaveNode(node);
        }

        public void PublishNode(INodeWrapper node, CultureInfo culture, DateTime startDate, DateTime? endDate)
        {
            NodeManager.PublishNode(node.InnerNode.ID, culture, startDate, endDate);
        }

        public void SaveAndPublishNode(INodeWrapper node, CultureInfo culture, DateTime startDate, DateTime? endDate)
        {
            NodeManager.SaveNode(node);
            NodeManager.PublishNode(node.InnerNode.ID, culture, startDate, endDate);
        }

        public void SaveNodeHandlingPositionAndParentNodeId(INodeWrapper node, Guid? parentNodeId, short? position)
        {
            if (parentNodeId.HasValue)
                node.InnerNode.ParentID = parentNodeId;
            if (position.HasValue)
                node.InnerNode.Position = position.Value;
            NodeManager.SaveNode(node);
        }

        public void SaveAndPublishNodeHandlingPositionAndParentNodeId(INodeWrapper node, Guid? parentNodeId, short? position, CultureInfo culture, DateTime startDate, DateTime? endDate)
        {
            if (parentNodeId.HasValue)
                node.InnerNode.ParentID = parentNodeId;
            if (position.HasValue)
                node.InnerNode.Position = position.Value;
            NodeManager.SaveNode(node);
            NodeManager.PublishNode(node.InnerNode.ID, culture, startDate, endDate);
        }
    }
}